package com.light.remote.client.helpingclasses;
///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package com.light.remote.helpingclasses;
//
//import java.io.DataInputStream;
//import java.io.DataOutputStream;
//import java.io.IOException;
//import java.util.Vector;
//import javax.bluetooth.BluetoothStateException;
//import javax.bluetooth.DeviceClass;
//import javax.bluetooth.DiscoveryAgent;
//import javax.bluetooth.DiscoveryListener;
//import javax.bluetooth.LocalDevice;
//import javax.bluetooth.RemoteDevice;
//import javax.bluetooth.ServiceRecord;
//import javax.bluetooth.UUID;
//import javax.microedition.io.Connector;
//import javax.microedition.io.StreamConnection;
//import javax.microedition.lcdui.Command;
//import javax.microedition.lcdui.CommandListener;
//import javax.microedition.lcdui.Displayable;
//import javax.microedition.lcdui.List;
//
///**
// *
// * @author MH
// */
//public class ServiceManager implements DiscoveryListener{
//    private static final String SERVER_NAME = "MHRemoteServer";
//    private LocalDevice localDevice;
//    private DiscoveryAgent agent;
//    private Vector devices = new Vector(0,1);
//    private Vector servers = new Vector(0,1);
//    private int noOfCheckedDevices=0;
//    private UUID[] uuids = {new UUID(0x1101)};
//    private int[] attrs = {0x0100, 0x0003, 0x0004};
//    private RemoteDevice remote;
//    private MainMIDlet mid;
//    
//    private String myFriendlyName;
//    private String myBluetoothAddress;
//    private String pcFriendlyName;
//    private String pcBluetoothAddress;
//    private boolean serverFound=true;
//    private StreamConnection conn;
//    private DataInputStream in;
//    private DataOutputStream out;
//    
//    public ServiceManager(MainMIDlet mid){
//        this.mid=mid;
//    }
//    
//    public void searchForAllDevices(){
//        try {
//            echo("Searching ...");
//            localDevice = LocalDevice.getLocalDevice();
//            localDevice.setDiscoverable(DiscoveryAgent.GIAC);
//            myFriendlyName=localDevice.getFriendlyName();
//            myBluetoothAddress=localDevice.getBluetoothAddress();
//            agent = localDevice.getDiscoveryAgent();
//            agent.startInquiry(DiscoveryAgent.GIAC, this);
//
//        } catch (Exception ex) {
//            echo("error1: " + ex.getMessage());
//        }
//    }
//
//    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
//        devices.addElement(btDevice);
//    }
//
//    public void inquiryCompleted(int discType) {
//        if (discType == INQUIRY_COMPLETED) {
//            getAllServers();
//        } else {
//            echo("inquiry error");
//        }
//    }
//
//    public void servicesDiscovered(int transID, ServiceRecord[] servRecord){
//        for (int j = 0; j < servRecord.length; j++) {
//            if (((String) servRecord[j].getAttributeValue(0x0100).getValue()).equals(SERVER_NAME)){
//                servers.addElement(servRecord[j]);
//            }
//        }
//    }
//
//    public void serviceSearchCompleted(int transID, int respCode) {
//        noOfCheckedDevices++;
//        getAllServers();
//    }
//    
//    private void getAllServers(){    
//        if(noOfCheckedDevices<devices.size()){
//            remote = (RemoteDevice) devices.elementAt(noOfCheckedDevices);
//            try {
//                agent.searchServices(attrs, uuids, remote, ServiceManager.this);
//            } catch (BluetoothStateException ex) {
//                echo("Check Errors:"+ex.getMessage());
//            }
//        }
//        else checkFoundServers();
//    }
//    
//    private void checkFoundServers(){
//        if(servers.isEmpty()) {
//            serverFound=false;
//            echo("No servers found");
//        }
//        else if(servers.size()==1) startService((ServiceRecord)servers.elementAt(0));
//        else chooseServer();
//    }
//    
//    private void chooseServer(){
//        final List list = new List("devices list", List.IMPLICIT);
//        for(int i=0;i<servers.size();i++){
//            String name;
//            try {
//                name = ((ServiceRecord)servers.elementAt(i)).getHostDevice().getFriendlyName(false);
//            } catch (IOException ex) {
//                echo(ex.getMessage());
//                name = ((ServiceRecord)servers.elementAt(i)).getHostDevice().getBluetoothAddress();
//            }
//            list.append(name, null);
//        }
//        list.setCommandListener(new CommandListener() {
//            public void commandAction(Command c, Displayable d) {
//                startService((ServiceRecord)servers.elementAt(list.getSelectedIndex()));
//            }
//        });
//        mid.getDisplay().setCurrent(list);
//    }
//    
//    private void startService(ServiceRecord device){
//        String url = device.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
//        try {
//            conn = (StreamConnection) Connector.open(url);
//            out = conn.openDataOutputStream();
//            in = conn.openDataInputStream();
//            mid.getDisplay().setCurrent(mid.getMainScreen());
//            pcFriendlyName=device.getHostDevice().getFriendlyName(false);
//            pcBluetoothAddress=device.getHostDevice().getBluetoothAddress();
//        } catch (IOException ex) {
//            echo(ex.getMessage());
//        }
//    }
//    
//    private void echo(String text){
//        mid.getErrorStringItem().setText(text);
//    }
//
//    public StreamConnection getConn() {
//        return conn;
//    }
//
//    public String getMyBluetoothAddress() {
//        return myBluetoothAddress;
//    }
//
//    public String getMyFriendlyName() {
//        return myFriendlyName;
//    }
//
//    public String getPcFriendlyName() {
//        return pcFriendlyName;
//    }
//
//    /**
//     * @return the in
//     */
//    public DataInputStream getIn() {
//        return in;
//    }
//
//    /**
//     * @return the out
//     */
//    public DataOutputStream getOut() {
//        return out;
//    }
//}
